package com.lzb.musicplayer.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.adapter.PlayQueueAdapter;
import com.lzb.musicplayer.audio.AudioPlayerManager;
import com.lzb.musicplayer.bean.GroupMusicInfo;
import com.lzb.musicplayer.bean.MusicFile;
import com.lzb.musicplayer.databinding.DialogBottomBinding;
import com.lzb.musicplayer.utils.MusicScanner;
import com.lzb.musicplayer.utils.MusicScannerUtil;
import com.lzb.musicplayer.utils.SpUtils;

import java.util.ArrayList;
import java.util.List;

public class BottomDialog extends BaseSmartDialog<DialogBottomBinding> implements View.OnClickListener, AudioPlayerManager.PlaybackStateListener {

    private Activity activity;
    private List<GroupMusicInfo> groupedList;
    private PlayQueueAdapter playQueueAdapter;
    private DeleteDialog deleteDialog;
    private AudioPlayerManager playerManager;


    public BottomDialog(@NonNull Context context) {
        super(context);
        this.activity = (Activity) context;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_bottom;
    }

    @Override
    protected void initView(View view) {
        playerManager = AudioPlayerManager.getInstance(getContext());
        playerManager.addListener(this);

        if (playerManager.getCurrentTrack()!= null) {
            mDataBinding.llPlayingNow.setVisibility(View.VISIBLE);
            MusicFile musicFile = playerManager.getCurrentTrack();
            Bitmap albumCover = MusicScannerUtil.getAlbumArt(getContext(), musicFile.getAlbumCover());
            Bitmap bit = MusicScannerUtil.getAlbumArt(getContext(), musicFile.getArtistCover());
            mDataBinding.ivImg.setImageResource(R.drawable.gequ);
            if (albumCover != null) {
                mDataBinding.ivImg.setImageBitmap(albumCover);
            } else {
                if (bit != null) {
                    mDataBinding.ivImg.setImageBitmap(bit);
                }
            }
            if (!TextUtils.isEmpty(musicFile.getTitle())) {
                mDataBinding.tvName.setText(musicFile.getTitle());
            }
        }

        List<MusicFile> list = SpUtils.getInstance().getList("listdl");
        groupedList = MusicScanner.groupByProperty(activity, list);

        mDataBinding.rvPlayList.setLayoutManager(new LinearLayoutManager(getContext()));
        playQueueAdapter = new PlayQueueAdapter();
        mDataBinding.rvPlayList.setAdapter(playQueueAdapter);

        playQueueAdapter.addChildClickViewIds(R.id.ivPlayQueueItemDelete);

        playQueueAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                startPlay(position);
            }
        });

        playQueueAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (deleteDialog == null) deleteDialog = new DeleteDialog(getContext());
                deleteDialog.setListener(new DeleteDialog.Ilistener() {
                    @Override
                    public void clickDelete() {
                        List<GroupMusicInfo> data = playQueueAdapter.getData();
                        if (data != null && position < data.size()) {
                            data.remove(position);
                            playQueueAdapter.notifyDataSetChanged();
                            List<MusicFile> musicFiles = new ArrayList<>();
                            for (int i = 0; i < data.size(); i++) {
                                musicFiles.add(data.get(i).getList().get(0));
                            }
                            SpUtils.getInstance().putList("listdl", musicFiles);
                        }
                    }
                });
                deleteDialog.show();
            }
        });

        playQueueAdapter.setList(groupedList);
        if (groupedList != null && groupedList.size() > 0) {
            mDataBinding.rvPlayList.setVisibility(View.VISIBLE);
            mDataBinding.tvPlayNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPlayList.setVisibility(View.GONE);
            mDataBinding.tvPlayNoData.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
    }

    private void startPlay(int index) {
        List<MusicFile> listAll = new ArrayList<>();
        List<GroupMusicInfo> data = playQueueAdapter.getData();
        for (int i = 0; i < data.size(); i++) {
            List<MusicFile> list1 = data.get(i).getList();
            listAll.addAll(list1);
        }
        mDataBinding.audioPlayerView.setList(listAll);
        if (index < data.size()) {
            boolean play = data.get(index).isPlay();
            mDataBinding.audioPlayerView.startPlayByPosition(index);
            mDataBinding.audioPlayerView.startPlay();
        }

    }


    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    @Override
    public void onPlaybackStateChanged(boolean isPlaying) {
        if (isPlaying) {
            if (playerManager.getCurrentTrack() != null) {
                mDataBinding.llPlayingNow.setVisibility(View.VISIBLE);
                MusicFile musicFile = playerManager.getCurrentTrack();
                Bitmap albumCover = MusicScannerUtil.getAlbumArt(getContext(), musicFile.getAlbumCover());
                Bitmap bit = MusicScannerUtil.getAlbumArt(getContext(), musicFile.getArtistCover());
                mDataBinding.ivImg.setImageResource(R.drawable.gequ);
                if (albumCover != null) {
                    mDataBinding.ivImg.setImageBitmap(albumCover);
                } else {
                    if (bit != null) {
                        mDataBinding.ivImg.setImageBitmap(bit);
                    }
                }
                if (!TextUtils.isEmpty(musicFile.getTitle())) {
                    mDataBinding.tvName.setText(musicFile.getTitle());
                }
            }
        } else {
            mDataBinding.llPlayingNow.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPlaybackStateChanged(int state) {

    }

    @Override
    public void onProgressUpdated(long position, long duration) {

    }

    @Override
    public void onTrackChanged(MusicFile currentTrack) {

    }

    public interface Ilistener {
        void clickItem(int pos);
    }
}
